'use strict';
const Controller = require('egg').Controller;

class OrderController extends Controller {
  /**
       * @api {Post} /api/order 添加订单
       * @apiGroup 订单
       * @apiParam {String}  goods_info      商品组信息    （必填,格式为[{id:1,num:1},{id:2,num:2}]）
       * @apiParam {INTEGER} addr_id         地址id   （必填）
       * @apiParam {INTEGER} shoppingCartIds 购物车id组(就是购物车每个商品信息的id) （选填,格式[1,2,3]）
       * @apiParamExample 参数模板
       {
          addr_id: 1,
          goods_info: [
              {id:1, num: 1},
              {id:2, num: 2}
          ],
          [1,2,3]
        }
       * @apiSuccessExample 成功返回
       * {
       *    "msg":'下单成功'
       * }
       * @apiErrorExample 错误返回
       * {
       *  "msg":"添加失败"
       * }
       *
       */

  async create() {
    const { ctx, service } = this;
    const dataObj = ctx.request.body;
    if (new ctx.helper.checkData(this, 'goods_info', 'addr_id').checkDataRes) {
      ctx.body = await service.shop.order.create(dataObj);
    }
  }

  // 获取订单列表
  /**
       * @api {Get} /api/order 获取订单列表
       * @apiGroup 订单
       * @apiParam {int} page   页数 (选填)
       * @apiParam {int} limit   每页多少条数据 (选填)
       * @apiParam {String} order_id 订单号(筛选用)
       * @apiParam {String}  status  订单状态(-1：已删除，0：已过期，1:已下单，2:已支付，3:已发货, 4:退货中，5:退款中，6:已完成，7：已取消)
       * @apiParam {String} start 起始时间 (yyyy-mm-dd，defaultvalue='1970-01-01')
       * @apiParam {String} end 结束时间 (yyyy-mm-dd，查询当天需要日期+1天,defaultvalue='9999-12-31')
       * @apiParamExample 参数模板
       {}
       * @apiSuccessExample  成功返回
       {
          "msg": "成功操作",
          "result": {
              "count": 3,
              "rows": [
                  {
                      "id": 1,
                      "order_id": "1598338511927",
                      "user_id": "1",
                      "shops_id": "0",
                      "shops_name": "",
                      "goods_info": [
                          {
                              "id": 1,
                              "goods_name": "潮流男裤",
                              "price": 99,
                              "s_user": {
                                  "id": 1,
                                  "name": "陈商家"
                              },
                              "s_photos": [
                                  {
                                      "id": 1,
                                      "path": "http://127.0.0.1:7002/public/uploads/1598338128140-13e05d63a735989e9c548e74478ae667.jpg"
                                  }
                              ],
                              "num": "1"
                          }
                      ],
                      "status": "1",
                      "logistics": null,
                      "addr_id": 1,
                      "invoice": null,
                      "total_price": "99.00",
                      "mode_of_payment": null,
                      "payment": null,
                      "order_time": "2020-08-25T06:55:03.000Z",
                      "payment_time": null,
                      "await_payment_time": null,
                      "createdAt": "2020-08-25T06:55:03.000Z",
                      "updatedAt": "2020-08-25T06:55:03.000Z"
                  },
                  {
                      "id": 2,
                      "order_id": "1598338552531",
                      "user_id": "1",
                      "shops_id": "0",
                      "shops_name": "",
                      "goods_info": [
                          {
                              "id": 1,
                              "goods_name": "潮流男裤",
                              "price": 99,
                              "s_user": {
                                  "id": 1,
                                  "name": "陈商家"
                              },
                              "s_photos": [
                                  {
                                      "id": 1,
                                      "path": "http://127.0.0.1:7002/public/uploads/1598338128140-13e05d63a735989e9c548e74478ae667.jpg"
                                  }
                              ],
                              "num": 4
                          }
                      ],
                      "status": "1",
                      "logistics": null,
                      "addr_id": 1,
                      "invoice": null,
                      "total_price": "396.00",
                      "mode_of_payment": null,
                      "payment": null,
                      "order_time": "2020-08-25T06:55:43.000Z",
                      "payment_time": null,
                      "await_payment_time": null,
                      "createdAt": "2020-08-25T06:55:43.000Z",
                      "updatedAt": "2020-08-25T06:55:43.000Z"
                  },
                  {
                      "id": 3,
                      "order_id": "1598338653846",
                      "user_id": "1",
                      "shops_id": "1",
                      "shops_name": "陈商家",
                      "goods_info": [
                          {
                              "id": 1,
                              "goods_name": "潮流男裤",
                              "price": 99,
                              "s_user": {
                                  "id": 1,
                                  "name": "陈商家"
                              },
                              "s_photos": [
                                  {
                                      "id": 1,
                                      "path": "http://127.0.0.1:7002/public/uploads/1598338128140-13e05d63a735989e9c548e74478ae667.jpg"
                                  }
                              ],
                              "num": 2
                          }
                      ],
                      "status": "1",
                      "logistics": null,
                      "addr_id": 1,
                      "invoice": null,
                      "total_price": "198.00",
                      "mode_of_payment": null,
                      "payment": null,
                      "order_time": "2020-08-25T06:57:24.000Z",
                      "payment_time": null,
                      "await_payment_time": null,
                      "createdAt": "2020-08-25T06:57:24.000Z",
                      "updatedAt": "2020-08-25T06:57:24.000Z"
                  },
              ]
          }
      }
       * @apiErrorExample  错误返回
       * {
       *   "msg": "请登录后再进行操作"
       * }
       */

  async index() {
    const { ctx, service } = this;
    ctx.body = await service.shop.order.index();
  }
  // 获取订单单条信息
  async ordercount() {
    const { ctx, service } = this;
    ctx.body = await service.shop.order.ordercount();
  }
  /**
       * @api {Get} /api/order/:id 获取订单单条信息
       * @apiGroup 订单
       * @apiParamExample 参数模板
       {}
       * @apiSuccessExample  成功返回
      {
          "msg": "成功操作",
          "result": {
              "id": 1,
              "order_id": "1598338511927",
              "user_id": "1",
              "shops_id": "0",
              "shops_name": "",
              "goods_info": [
                  {
                      "id": 1,
                      "goods_name": "潮流男裤",
                      "price": 99,
                      "s_user": {
                          "id": 1,
                          "name": "陈商家"
                      },
                      "s_photos": [
                          {
                              "id": 1,
                              "path": "http://127.0.0.1:7002/public/uploads/1598338128140-13e05d63a735989e9c548e74478ae667.jpg"
                          }
                      ],
                      "num": "1"
                  }
              ],
              "status": "1",
              "logistics": null,
              "addr_id": 1,
              "invoice": null,
              "total_price": "99.00",
              "mode_of_payment": null,
              "payment": null,
              "order_time": "2020-08-25T06:55:03.000Z",
              "payment_time": null,
              "await_payment_time": null,
              "createdAt": "2020-08-25T06:55:03.000Z",
              "updatedAt": "2020-08-25T06:55:03.000Z",
              "s_address": {
                  "id": 1,
                  "user_id": 1,
                  "name": "陈国宇",
                  "phone": "13631137059",
                  "area": "2,52,500",
                  "area_name": "北京省市辖区东城区",
                  "desc": "十多个撒了个骄傲了",
                  "post_code": "0000",
                  "default_set": "1",
                  "status": "1",
                  "createdAt": "2020-08-25T06:50:37.000Z",
                  "updatedAt": "2020-08-25T06:50:37.000Z"
              }
          }
      }
       * @apiErrorExample  错误返回
       * {
       *   "msg": "请登录后再进行操作"
       * }
       */

  async show() {
    const { ctx } = this;
    const id = ctx.params.id;
    ctx.body = await this.service.shop.order.show(id);
  }
  // 更新订单信息
  /**
         * @api {update} /api/order/:id 更新订单信息
         * @apiGroup 订单
         * @apiParam {String}  name             // 商品名称    （必填）
         * @apiParamExample 参数模板
         {
          }
         * @apiSuccessExample  成功返回
         {
        }
         * @apiErrorExample  错误返回
         * {
         *   "msg": "请登录后再进行操作"
         * }
         */

  async update() {
    const { ctx, service } = this;
    const dataObj = ctx.request.body;

    const id = +ctx.params.id;
    console.log('dataObjdataObj');
    console.log(dataObj);
    ctx.body = await service.shop.order.update(id, dataObj);
  }
  /**
       * @api {delete} /api/order/:id 删除订单
       * @apiGroup 订单
       * @apiParamExample 参数模板
       {}
       * @apiSuccessExample 成功返回
       * {
       *    "msg":'删除成功'
       * }
       *
       * @apiErrorExample 错误返回
       * {
       *  "msg":"删除失败"
       * }
       *
       */

  async destroy() {
    const { ctx, service } = this;
    const id = ctx.params.id;
    ctx.body = await service.shop.order.destroy(id);
  }
  /**
     * 
     * @api {POST} /api/createLogistics 创建物流信息(需要token 如果创建的物流信息长度小于6可重新创建)
     * @apiName 创建物流信息
     * @apiGroup 订单
     * @apiVersion  1.0.0
     * 
     * 
     * @apiParam  {String} order_id 订单id
     * 
     * @apiParamExample  {type} Request-Example:
        {
            "order_id":"1635321164050"
        }
     * 
     * 
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": [
                {
                    "time": "2021.10.30 07:52:41",           //送达时间
                    "context": "您的包裹已经送达至本人/家人，如有疑问可致电菜鸟驿站人员或菜鸟客服9519666",  //内容描述
                    "address": "北京省市辖区东城区天河路",      //收货地址
                    "phone": "13622222222"                    //收货人手机号
                },
                {
                    "time": "2021.10.29 23:52:41",
                    "context": "您的包裹正在由菜鸟驿站上门派送中,如有问题可致电菜鸟驿站人员或菜鸟客服9519666"
                },
                {
                    "time": "2021.10.29 15:52:41",
                    "context": "【汕头市】 快件到达【汕头贵陈网点】"
                },
                {
                    "time": "2021.10.29 07:52:41",
                    "context": "【揭阳市】快件离开【揭阳转运中心】已发往【汕头贵陈网点】"
                },
                {
                    "time": "2021.10.28 23:52:41",
                    "context": "【揭阳市】 快件到达【揭阳转运中心】"
                },
                {
                    "time": "2021.10.28 15:52:41",
                    "context": "【福州市】快件离开【福州转运中心】已发往【揭阳转运中心】"
                },
                {
                    "time": "2021.10.28 07:52:41",
                    "context": "【福州市】 快件到达【福州转运中心】"
                },
                {
                    "time": "2021.10.27 23:52:41",
                    "context": "【福州市】快件离开【福州市福清网点】已发往【福州转运中心】"
                },
                {
                    "time": "2021.10.27 15:52:41",
                    "context": "【福州市】【福州市福清网点】的何富松已取件。投诉电话:"
                }
            ]
        }
     * 
     * 
     */
  //创建物流信息
  async createLogistics() {
    const { app, ctx, service } = this;
    const data = ctx.request.body;
    let checkRes = this.ctx.helper.dataCheck('order_id');
    //如果有缺少参数
    if (checkRes) {
      return ctx.helper.lackModule(checkRes);
    }
    try {
      let res = await service.shop.order.createLogistics();
      if (res == '已存在物流信息') {
        return ctx.helper.errorModule(res);
      }
      return ctx.helper.successModule(res);
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('创建失败');
    }
  }

  /**
     * 
     * @api {GET} /api/getLogistics?order_id=1635499918826 根据订单查找物流信息
     * @apiName 根据订单查找物流信息
     * @apiGroup 订单
     * @apiVersion  1.0.0
     * 
     * 
     * @apiParam  {String} order_id 订单id
     * 
     * 
     * 
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": [
                {
                    "time": "2021.11.01 01:31:49",      //送达时间
                    "context": "您的包裹已经送达至本人/家人，如有疑问可致电菜鸟驿站人员或菜鸟客服9519666",  //物流信息
                    "address": "北京省市辖区东城区天河路",  //收货地址
                    "phone": "13622222222"              //收货人手机号
                },
                {
                    "time": "2021.10.31 17:31:49",
                    "context": "您的包裹正在由菜鸟驿站上门派送中,如有问题可致电菜鸟驿站人员或菜鸟客服9519666"
                },
                {
                    "time": "2021.10.31 09:31:49",
                    "context": "您的快递已到达【临沂兰山公司】，上一站【临沂分拨中心】"
                },
                {
                    "time": "2021.10.31 01:31:49",
                    "context": "您的快递已发件，下一站【临沂兰山公司】"
                },
                {
                    "time": "2021.10.30 17:31:49",
                    "context": "您的快递已发件，下一站【临沂分拨中心】"
                },
                {
                    "time": "2021.10.30 09:31:49",
                    "context": "您的快递已到达【济南分拨中心】，上一站【济南项目部】"
                },
                {
                    "time": "2021.10.30 01:31:49",
                    "context": "您的快递已发件，下一站【济南分拨中心】"
                },
                {
                    "time": "2021.10.29 17:31:49",
                    "context": "您的快递已由【王峰】收件"
                }
            ]
        }
     * 
     * 
     */
  //获取物流信息
  async getLogistics() {
    const { app, ctx, service } = this;
    const data = ctx.request.body;
    let checkRes = this.ctx.helper.dataCheck('order_id');
    //如果有缺少参数
    if (checkRes) {
      return ctx.helper.lackModule(checkRes);
    }
    let res = await service.shop.order.getLogistics();
    if (res == '没有物流信息') {
      return ctx.helper.errorModule(res);
    }
    return ctx.helper.successModule(res);
  }

  /**
   *
   * @api {post} /api/orderpay 支付订单
   * @apiName 支付订单
   * @apiGroup 订单
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {Number} id 订单ID（不是订单号）
   *
   * 
   * @apiParamExample  {type} Request-Example:
   * {
   *     "id": 2511,
   * }
   *
   *
   * @apiSuccessExample {type} Success-Response:
{
    "code": 200,
    "msg": "支付成功",
    "data": {
        "id": 2511,
        "order_id": "1657853533310",
        "user_id": "1786",
        "project_id": 209,
        "shops_id": "188",
        "shops_name": "mobileshop",
        "goods_info": "[{\"id\":3374,\"goods_name\":\"xx衣服\",\"price\":998,\"sale_price\":9.8,\"sold_num\":608,\"desc\":null,\"s_admin\":{\"id\":188,\"name\":\"mobileshop\"},\"s_goods_photos\":[{\"id\":15785,\"path\":\"http://shops-1251395798.cos.ap-nanjing.myqcloud.com/_%E9%BB%98%E8%AE%A4%E9%A1%B9%E7%9B%AE_1657507665747_src%3Dhttp___imgservice.suning.cn_uimg1_b2c_image_qorMjx4W5AxBT9a9F4i7tQ.jpg_800w_800h_4e%26refer%3Dhttp___imgservice.suning.webp\"}],\"updatedAt\":\"2022-07-15T02:52:10.749Z\",\"num\":100}]",
        "status": 2,
        "logistics": null,
        "addr_id": 1867,
        "invoice": null,
        "total_price": "980.00",
        "mode_of_payment": null,
        "payment": null,
        "order_time": "1657853530869",
        "payment_time": null,
        "await_payment_time": null,
        "createdAt": "2022-07-15T02:52:10.000Z",
        "updatedAt": "2022-07-15T03:50:18.226Z"
    }
}
   *
   *
   */
  async pay() {
    const { ctx, app, service } = this;
    const data = ctx.request.body;

    if (!data.id) return ctx.helper.lackModule('缺少订单ID');
    data.status = data.status ? '2' : '1';

    const [result, err] = await service.shop.order.pay();

    return result
      ? ctx.helper.successModule('支付成功', result)
      : ctx.helper.errorModule(err);
  }
}

module.exports = OrderController;
